//
//  CNavController.swift
//  CTools
//
//  Created by xhl_dabaoMac on 2021/11/18.
//

import UIKit


public class CNavController: UIViewController {

    public var subController : UIViewController?//要添加的控制器
    
    public var navViews : [UIView]?//导航的子view
    public var startLocation = CLocation.center//从哪里开始布局
    public var backImage : UIImage? = Creator.getImage("back_black")
    
    public var navigationBar : CNavigationBar = CNavigationBar(frame: .zero)
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        addNav()
        addCon()

    }
    func addNav(){
        //添加导航头
        view.addSubview(navigationBar)
        let statusHeight = UIApplication.shared.statusBarFrame.size.height
        let barHeight = self.navigationController?.navigationBar.frame.size.height

        let navigationHeight = statusHeight + (barHeight ?? 0)
        navigationBar.snp.makeConstraints { make in
            make.height.equalTo(navigationHeight)
            make.left.right.top.equalToSuperview()
        }
       //添加返回键
        if let bi = backImage{
            navigationBar.addLeftView(Creator.getImageView(image: bi)) {
                [weak self] in
                self?.navigationController?.popViewController(animated: true)
            }
        }
        //添加其他视图
        if let nViews = navViews{
            navigationBar.contentView.layoutMultipleView(startlocation: startLocation, views: nViews){ index in}
            
        }else{
            if let navTitle = title{
                navigationBar.addTitle(navTitle)
            }
        }

    }
    
    /// 添加控制器页面
    func addCon(){
        guard let vc = subController else {
            print("需要传入一个实例控制器")
            return;
        }
        addChild(vc)
        view.addSubview(vc.view)
        vc.view.snp.makeConstraints { make in
            make.top.equalTo(navigationBar.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    deinit {
        debugPrint("deinit:\(self)")
    }
}
